package com.ruoyi.ggsearch.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanValidators;
import com.ruoyi.ggsearch.domain.GgKeyWords;
import com.ruoyi.ggsearch.mapper.GgKeyWordsMapper;
import com.ruoyi.ggsearch.service.IGgKeyWordsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 关键字信息Service业务层处理
 * 
 * @author yangjin
 * @date 2023-04-07
 */
@Service
public class GgKeyWordsServiceImpl implements IGgKeyWordsService 
{
    private static final Logger log = LoggerFactory.getLogger(GgKeyWordsServiceImpl.class);
    @Autowired
    private GgKeyWordsMapper ggKeyWordsMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询关键字信息
     * 
     * @param keyId 关键字信息主键
     * @return 关键字信息
     */
    @Override
    public GgKeyWords selectGgKeyWordsByKeyId(Long keyId)
    {
        return ggKeyWordsMapper.selectGgKeyWordsByKeyId(keyId);
    }

    /**
     * 查询关键字信息列表
     * 
     * @param ggKeyWords 关键字信息
     * @return 关键字信息
     */
    @Override
    public List<GgKeyWords> selectGgKeyWordsList(GgKeyWords ggKeyWords)
    {
        return ggKeyWordsMapper.selectGgKeyWordsList(ggKeyWords);
    }

    /**
     * 新增关键字信息
     * 
     * @param ggKeyWords 关键字信息
     * @return 结果
     */
    @Override
    public int insertGgKeyWords(GgKeyWords ggKeyWords)
    {
        return ggKeyWordsMapper.insertGgKeyWords(ggKeyWords);
    }

    /**
     * 修改关键字信息
     * 
     * @param ggKeyWords 关键字信息
     * @return 结果
     */
    @Override
    public int updateGgKeyWords(GgKeyWords ggKeyWords)
    {
        return ggKeyWordsMapper.updateGgKeyWords(ggKeyWords);
    }

    /**
     * 批量删除关键字信息
     * 
     * @param keyIds 需要删除的关键字信息主键
     * @return 结果
     */
    @Override
    public int deleteGgKeyWordsByKeyIds(Long[] keyIds)
    {
        return ggKeyWordsMapper.deleteGgKeyWordsByKeyIds(keyIds);
    }

    /**
     * 删除关键字信息信息
     * 
     * @param keyId 关键字信息主键
     * @return 结果
     */
    @Override
    public int deleteGgKeyWordsByKeyId(Long keyId)
    {
        return ggKeyWordsMapper.deleteGgKeyWordsByKeyId(keyId);
    }

    @Override
    public String importKeyWords(List<GgKeyWords> keyWordsList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(keyWordsList) || keyWordsList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GgKeyWords keyWords : keyWordsList)
        {
            try
            {
                // 验证是否存在这个关键字
                GgKeyWords d = ggKeyWordsMapper.selectGgKeyWordsByName(keyWords.getKeyWord());
                if (StringUtils.isNull(d))
                {
                    BeanValidators.validateWithException(validator, keyWords);
                    ggKeyWordsMapper.insertGgKeyWords(keyWords);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、关键字 " + keyWords.getKeyWord() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, keyWords);
                    ggKeyWordsMapper.updateGgKeyWords(keyWords);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、关键字 " + keyWords.getKeyWord() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、关键字 " + keyWords.getKeyWord() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、关键字 " + keyWords.getKeyWord() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public void deleteGgKeyWordsAll() {
        ggKeyWordsMapper.deleteGgKeyWordsAll();
    }
}
